import React from 'react';
import { Router, Route, Redirect, Switch } from 'react-router-dom';
import history from './utils/history';
import Loading from './components/Loading';
// 路由懒加载
const Login = React.lazy(() => import('@/pages/Login'));
const Layout = React.lazy(() => import('@/pages/Layout'));
const Article = React.lazy(() => import('@/pages/Article'));
const Chat = React.lazy(() => import('@/pages/Profile/Chat'));
const ProfileEdit = React.lazy(() => import('@/pages/Profile/Edit'));
const ProfileFeedback = React.lazy(() => import('@/pages/Profile/Feedback'));
const Search = React.lazy(() => import('@/pages/Search'));
const SearchResult = React.lazy(() => import('@/pages/Search/Result'));

export default function App() {
  return (
    // 把 `BrowserRouter` 替换成 `Router + history` 方式：
    <Router history={history}>
      {/*  React.suspence:包住Switch       fallback是一个必传属性 */}
      <React.Suspense fallback={<Loading />}>
        <Switch>
          <Redirect exact from="/" to="/home" />
          <Route path="/login" component={Login} />
          {/* ------------------ */}
          <Route path="/search" component={Search} />
          <Route path="/article/:id" component={Article} />
          <Route path="/search/result" component={SearchResult} />
          <Route path="/profile/edit" component={ProfileEdit} />
          <Route path="/profile/feedback" component={ProfileFeedback} />
          <Route path="/profile/chat" component={Chat} />
          <Route path="/" component={Layout} />
        </Switch>
      </React.Suspense>
    </Router>
  );
}
